import axios from 'axios';
import qs from 'qs';
import {Toast, Indicator} from 'mint-ui';
import Client from '../js/common';
let addressUrl = location.href,
    webUrl = 'http://psaasweb.ccnfgame.com/hj/#/',//行家
    zdUrl = 'http://psaasweb.ccnfgame.com/im/#/',//终端对话
    centerUrl = 'http://psaasweb.ccnfgame.com/center/#/',//管家中心
    hgjUrl = 'http://psaasweb.ccnfgame.com/',
    URL = 'http://phjapi.ccnfgame.com/api/',
    payUrl = 'http://psaaspay.ccnfgame.com/pay/createorder',
    IMAPI = 'http://pimapi.ccnfgame.com/api/';
let bdUrl = 'http://psaasweb.ccnfgame.com/biaodian/#/'; //标点


let chatAppId = '123456789',
    HJappid = '3d02f97ebf683a',
    HJchannelcode = 'CC201801010001100000';

if(/\/\/saasweb/.test(addressUrl) || /\/\/ccnfgame/.test(addressUrl)){
   webUrl = 'http://saasweb.ccnfgame.com/hj/#/';
   zdUrl = 'http://ccnfgame.com/im/#/';
   centerUrl = "http://saasweb.ccnfgame.com/center/#/";
   hgjUrl = 'http://saasweb.ccnfgame.com/';
   URL = 'http://hjapi.ccnfgame.com/api/';
   payUrl = 'http://saaspay.ccnfgame.com/pay/createorder',
   IMAPI = 'http://imapi.ccnfgame.com/api/';
   bdUrl = 'http://ccnfgame.com/biaodian/#/'; //标点
}else if(process.env.NODE_ENV == 'development'){
  webUrl = 'http://localhost:8084/#/';
  zdUrl = 'http://localhost:8080/#/';
  centerUrl = 'http://localhost:8085/#/';
  hgjUrl = 'http://saasweb.ccnfgame.com/';
  URL = 'http://hjapi.ccnfgame.com/api/';
  payUrl = 'http://saaspay.ccnfgame.com/pay/createorder',
  IMAPI = 'http://imapi.ccnfgame.com/api/';
  bdUrl = 'http://ccnfgame.com/biaodian/#/'; //标点
}else{
  //正式行家appid
  HJappid = '8d7a877e5a5ced',
  HJchannelcode = 'CC201801010001100000';
}



const urlSrting = new Client.getQueryString();
let channelCode = urlSrting.channelcode,
    urlAppid = urlSrting.appid,
    storeChannel = Client.getStore('CHANNELCODE'),
    storeAppid = Client.getStore('APPID');
if(channelCode&&urlAppid){
  HJchannelcode = channelCode;
  HJappid = urlAppid;
  Client.setStore('CHANNELCODE',HJchannelcode);
  Client.setStore('APPID',HJappid);
}else if(storeChannel&&storeAppid){
  HJchannelcode = storeChannel;
  HJappid = storeAppid;
}   

// console.log('传过来的ID',this.messageid);
// let _AppId = "123456789";
// let _Identifier = "10029971"
// let _OpenId = "9b8e0e3dcfa34d74bb5a884c903d7627";
// let _channelcode = "7dc26571d9ff4";
// let _Sig = "eJxlz11PgzAUgOF7fgXprcb0i8GWeIGExbmhkm06vWkQOtewFiztEI3-3YkaSTy373Nyct4d13XBarE8y-K8ssow09UcuBMXQHD6F*taFCwzjOjiX*SvtdCcZVvDdR*R53kYwqERBVdGbMWvgBCPxz4aiKYoWX-mG9DjPvURpkMinvuYxOtoFlu-usw88xCskrvQcjjaXRC62aflkyxn2TqmNHrspJ6mMhRx2M3r1p4gPRXXNmmWo6v7-BDkbbpRi9uXWM3fdpFVN3vZBuX54KQRkv-8hKlPMCF4UA9cN6JSPcAQeQgT*DXA*XA*AWjsXak_";
//   let loginStore = [{"OpenId":_OpenId},
//   {"OpenId":_OpenId,"Sig":_Sig},
//   {"OpenId":_OpenId,"Sig":_Sig,"channelcode": _channelcode}]
//   Client.setStore('login',loginStore);
//   let uData = {"AppId":_AppId,"OpenId":_OpenId};
//   Client.setStore('userLogin',uData);
//   let loginInfoData = {"sdkAppID":1400047124,"identifier":_Identifier,"accountType":18646,"userSig": _Sig,"checkLogin":{"AppId":_AppId,"OpenId":_OpenId}};
//   Client.setStore('loginInfo',loginInfoData);
//   let userDatas = {"AppId":_AppId,"Identifier":_Identifier,"OpenId":_OpenId,"Role":0,"SdkAppId":1400047124,"Sig": _Sig,"accountType":18646,"UserType":0}
//   Client.setStore('userDatas',userDatas);
//   Client.setStore('loginTime',new Date().getTime());

export {zdUrl,webUrl,centerUrl,chatAppId,HJappid,HJchannelcode,payUrl,IMAPI,hgjUrl,bdUrl};
//axios.defaults.headers.clientSecret = '123456';
//axios.defaults.headers.token = '123456';
axios.defaults.timeout = 15000;
axios.interceptors.request.use(config => {
  //在发送请求之前做某件事
   Indicator.open(); 
   if(config.method == 'post'){
     config.data.ChannelCode = HJchannelcode;
     config.data = qs.stringify(config.data);
   }
   return config;
 },error =>{
   Indicator.close(); 
   Toast("请求失败");
   return Promise.reject(error);
 });
 //返回状态判断(添加响应拦截器)
 axios.interceptors.response.use(res =>{
   //对响应数据做些事
   Indicator.close();
   if(res.data.ErrorCode !== 0){
     Toast(res.data.ErrorInfo);
     return Promise.reject(res.data);
   }
   return res.data;
 }, error => {
   Indicator.close(); 
   Toast("网络异常,请稍后再试");
   return Promise.reject(error);
 });
 //返回一个Promise(发送post请求)
 export function fetch(urls, params) {
    return axios.post(URL + urls, params,{headers:{clientSecret:'123456',token:'123456'}});
 }
 export function fetchGet(urls, params) {
  return axios.get(URL + urls, params,{headers:{clientSecret:'123456',token:'123456'}});
}
export function fetchOther(urls, params) {
  return axios.post(urls, params);
}
export function fetchOtherGet(urls, obj) {
  return axios.get(urls, {params:obj});
}